package com.ouran.common;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PropertiesUtils {
	/**
	 * 
	 * 得到文件的输入流
	 **/
	private static Properties file = new Properties();
	private Log logger = LogFactory.getLog(getClass());

	public void getFile(String fileName) {
		InputStream inputStream = getClass().getResourceAsStream(fileName);
		if (inputStream == null) {
			System.out.println(fileName + " is  exist!");
		} else {
			try {
				file.load(inputStream);
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * @param propertyName
	 *            读取 和写入
	 * @return key
	 */
	public String read(String propertyName) {
		return file.getProperty(propertyName);
	}

	public void write(String name, String value) {
		file.setProperty(name, value);
	}

	/**
	 * 关闭文件
	 */
	public void close() {
		try {
			OutputStream os = new FileOutputStream("conf.properties");
			file.store(os, null);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("jdbc.properties无法正常关闭");
		}
	}

}
